package com.code.coinprice.controllers;

import com.code.coinprice.consts.CommonConstant;
import com.code.coinprice.managers.CoinPriceManager;
import com.code.coinprice.models.CoinPriceSource;
import com.code.coinprice.services.CoinPriceService;
import com.code.coinprice.services.CoinPriceSourceService;
import com.code.coinprice.utils.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/coin-prices")
public class CoinPriceController {

    @Resource
    private CoinPriceSourceService coinPriceSourceService;

    @Resource
    private CoinPriceManager coinPriceManager;


    /**
     * 获取指定币种的币价
     */
    @GetMapping("/get-coin-price")
    public Object getCoinPrice(@RequestParam("source") String source, @RequestParam("coinType") String coinType) {
        source = source.toUpperCase();
        CoinPriceSource coinPriceSource = coinPriceSourceService.getCoinPriceSourceBySource(source);
        String coinPriceStr = CommonConstant.DEFAULT_COIN_PRICE;
        if (coinPriceSource != null) {
            CoinPriceService coinPriceService = coinPriceManager.getApplicationContext().getBean(source.toLowerCase() + CommonConstant.BEAN_NAME_SUFFIX, CoinPriceService.class);
            coinPriceStr = coinPriceService.getCoinPrice(source, coinType);
        }
        return ResponseUtil.ok(coinPriceStr);
    }

}
